package com.gt.andr.webbrowser;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class BroswerActivity extends Activity {
	/** Called when the activity is first created. */
	WebView webview;
	Button goBtn;
	EditText urlTxt;
	String htmlString1 = "<html><head><title>Testing .. title</title></head><body>a whole <b>bunch</b> of html stuff<table border=\"2\"><tr><td>Hello Web <i>View</i></td></tr></table><marquee>Hello marquee test</marquee></body></html>";
	String htmlJavaScript="<html><head><title>hello</title><script type=\"text/javascript\">        function sayHello(){                alert(\"Hello\");        }        function validateForm(){        var age=document.forms[\"myform\"].age.value;                if(isNaN(age)){                alert(\"Enter proper\");                return false;        }        if(age<18){                alert(\"Not eligible\");                return false;        }        else{        alert(\"Thats OK\");                return true;        }        }</script></head><body><h1>Welcome</h1><table border=\"3\"><tr><td><marquee>Hello marquee test</marquee> Hello Web <i>View</i></td></tr><tr><td><b onclick=\"sayHello()\">Click Me !!</b></td></tr><tr><td><form id=\"myform\" onsubmit=\"return validateForm()\">Age : <input name=\"age\" type=\"text\"><br><input type=\"submit\"></form></td></tr></table></body></html>";
	String htmlJs="<html>  <head>    <script type=\"text/javascript\">function sayHello(){alert(\"Hello\");}    </script>  </head>  <body>    <div onclick=\"sayHello()\">Click Me !!</div>  </body></html>";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browser);
		initComponents();
		loadMyHtml();

		// just a test
		String htmlTextStr = Html.fromHtml(htmlJs).toString();
		System.out.println(htmlTextStr);
	}

	private void loadMyHtml() {
		webview.loadData(htmlJs, "text/html", "utf-8");

	}

	private void initComponents() {
		goBtn = (Button) findViewById(R.id.goBtn);
		goBtn.setOnClickListener(new GoBtnListener());
		webview = (WebView) findViewById(R.id.webview);
		webview.getSettings().setJavaScriptEnabled(true);
		// webview.loadUrl("http://www.google.com");
		webview.setWebViewClient(new MyWebViewClient());
		webview.setWebChromeClient(new JsChromeClient());
		urlTxt = (EditText) findViewById(R.id.urlTxt);
	}
	private class JsChromeClient extends WebChromeClient{
		@Override
		public boolean onJsAlert(WebView view, String url, String message,
				final JsResult result) {
			new AlertDialog.Builder(BroswerActivity.this)  
            .setTitle("javaScript dialog")  
            .setMessage(message)  
            .setPositiveButton(android.R.string.ok,  
                    new AlertDialog.OnClickListener()  
                    {  
                        public void onClick(DialogInterface dialog, int which)  
                        {  
                            result.confirm();  
                        }  
                    })  
            .setCancelable(false)  
            .create()  
            .show();  
  
        return true;  
		}
	}
	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}

	private class GoBtnListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String url = urlTxt.getText().toString().trim();
			if (!url.equals("")) {
				webview.loadUrl(url);
				Log.i("", webview.getTitle());
			}
		}
	}
}